﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;

namespace CSSoft.VNTower.Domain
{
    [Serializable]
    public class WeeklyReportItem : BaseItem 
    {
        public int Year { get; set; }
        public int Week { get; set; }
        public DateTime From { get; set; }
        public DateTime Till { get; set; }
        public string Note { get; set; }
        public string LineManagerComment { get; set; }
        public string ReportStatus { get; set; }


        public UserItem Reporter { get; set; }
        public List<UserItem> ReportTo { get; set; }
        public List<UserItem> ReportCC { get; set; }

        public List<UserTaskItem> TasksAssigned { get; set; }
        public List<UserTaskItem> ActionRegister { get { return TasksAssigned.FindAll(p => p.Delay == true); } }
        public WeeklyReportItem() { TasksAssigned = new List<UserTaskItem>(); Reporter = new UserItem(); ReportTo = new List<UserItem>(); ReportCC = new List<UserItem>(); }


        public void GetItem(SPListItem item)
        {
            Item = item;
            try
            {
                Year = CS2Convert.ToInt(item["Year"]);
                Week = CS2Convert.ToInt(item["Week"]);
                From = CS2Convert.ToDateTime(item["From"]).Value;
                Till = CS2Convert.ToDateTime(item["Till"]).Value;
                Note = CS2Convert.ToString(Item["Note"]);
                LineManagerComment = CS2Convert.ToString(item["LineManagerComment"]);
                ReportStatus = CS2Convert.ToString(item["Status"]);
                Reporter = new UserItem { User = CS2Convert.ToSPUser(Item["Author"]) };
                SPFieldUserValueCollection reportTo = CS2Convert.ToSPFieldUserValueCollection(Item["ReportTo"]);
                if (reportTo != null)
                    foreach (SPFieldUserValue user in reportTo)
                        ReportTo.Add(new UserItem { User = user.User });
                SPFieldUserValueCollection reportCC = CS2Convert.ToSPFieldUserValueCollection(Item["ReportCC"]);
                if (reportCC != null)
                    foreach (SPFieldUserValue user in reportCC)
                        ReportCC.Add(new UserItem { User = user.User });

            }
            catch
            {
                Reporter = new UserItem() { User = CS2Web.CurrentUser };
            }
        }
    }
}
